package cate.game.opponent;

import java.util.List;

public class SectionIndex {

    public int indexBeg = 0;
    public int indexEnd = 0;

    /**
     * @param valueList 已经过排序的值list
     * @param noniusVal 游标值
     * @param wantNum 想要获取的理想数量
     */
    public SectionIndex(List<Long> valueList, long noniusVal, int wantNum) {
        calc(valueList, noniusVal, wantNum);
    }

    /**
     * @param valueArr 已经过排序的值数组
     * @param noniusVal 游标值
     * @param wantNum 想要获取的理想数量
     */
    public SectionIndex(long[] valueArr, long noniusVal, int wantNum) {
        calc(valueArr, noniusVal, wantNum);
    }

    private void calc(List<Long> valueList, long noniusVal, int wantNum) {
        long[] arr = new long[valueList.size()];
        for (int i = 0; i < valueList.size(); i++) {
            arr[i] = valueList.get(i);
        }
        calc(arr, noniusVal, wantNum);
    }

    private void calc(long[] valueArr, long noniusVal, int wantNum) {
        if (valueArr == null || valueArr.length <= 1) {
            return;
        }
        if (valueArr.length <= wantNum) {
            this.indexBeg = 0;
            this.indexEnd = valueArr.length - 1;
            return;
        }
        int watershedIndex = 0; // 分水岭
        boolean ASC = valueArr[valueArr.length - 1] >= valueArr[0]; // 升序
        boolean dealed = false;
        if (ASC) {
            if (noniusVal <= valueArr[0]) {
                this.indexBeg = 0;
                this.indexEnd = wantNum - 1;
                dealed = true;
            } else if (noniusVal >= valueArr[valueArr.length - 1]) {
                this.indexEnd = valueArr.length - 1;
                this.indexBeg = this.indexEnd - wantNum + 1;
                dealed = true;
            }
        } else {
            if (noniusVal >= valueArr[0]) {
                this.indexBeg = 0;
                this.indexEnd = wantNum - 1;
                dealed = true;
            } else if (noniusVal <= valueArr[valueArr.length - 1]) {
                this.indexEnd = valueArr.length - 1;
                this.indexBeg = this.indexEnd - wantNum + 1;
                dealed = true;
            }
        }
        if (!dealed) {
            for (int i = 1; i < valueArr.length; i++) {
                if (ASC && valueArr[i - 1] <= noniusVal && valueArr[i] >= noniusVal) {
                    watershedIndex = i;
                    break;
                } else if (!ASC && valueArr[i - 1] >= noniusVal && valueArr[i] <= noniusVal) {
                    watershedIndex = i;
                    break;
                }
            }
            int half = wantNum / 2;
            if (wantNum % 2 == 0) {
                this.indexBeg = watershedIndex - half;
                this.indexEnd = watershedIndex + half - 1;
            } else {
                this.indexBeg = watershedIndex - half;
                this.indexEnd = watershedIndex + half;
            }
        }
        if (this.indexBeg < 0) {
            int offset = 0 - this.indexBeg;
            this.indexBeg += offset;
            this.indexEnd += offset;
        }
        if (this.indexEnd >= valueArr.length) {
            int offset = valueArr.length - 1 - this.indexEnd;
            this.indexEnd += offset;
            this.indexBeg += offset;
            if (this.indexBeg < 0) {
                this.indexBeg = 0;
            }
        }
    }
}
